<html>
<head><meta charset="utf-8"><title>transmute vs from_raw_parts · t-lang/wg-unsafe-code-guidelines · Zulip Chat Archive</title></head>
<h2>Stream: <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/index.html">t-lang/wg-unsafe-code-guidelines</a></h2>
<h3>Topic: <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/transmute.20vs.20from_raw_parts.html">transmute vs from_raw_parts</a></h3>

<hr>

<base href="https://rust-lang.zulipchat.com">

<head><link href="https://rust-lang.github.io/zulip_archive/style.css" rel="stylesheet"></head>

<a name="174662639"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/transmute%20vs%20from_raw_parts/near/174662639" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Shnatsel <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/transmute.20vs.20from_raw_parts.html#174662639">(Sep 01 2019 at 13:52)</a>:</h4>
<p>I'm trying to determine if <code>let vector = std::mem::transmute::&lt;Vec&lt;NonZeroU8&gt;, Vec&lt;u8&gt;&gt;(vector);</code> is OK or do I need <code>Vec::from_raw_parts()</code> instead. <br>
The documentation on this exists, but it's confusing. <a href="https://doc.rust-lang.org/std/mem/fn.transmute.html#alternatives" target="_blank" title="https://doc.rust-lang.org/std/mem/fn.transmute.html#alternatives">https://doc.rust-lang.org/std/mem/fn.transmute.html#alternatives</a> has an example with a Vec being transmuted and claims it's UB, but doesn't explain why. I also don't understand why <code>.clone()</code> is called on the vector being transmuted, yet the comments claim that the operation does not copy the vector.</p>



<a name="174662888"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/transmute%20vs%20from_raw_parts/near/174662888" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Shnatsel <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/transmute.20vs.20from_raw_parts.html#174662888">(Sep 01 2019 at 14:00)</a>:</h4>
<p>Also the mem::forget at the end looks fishy. I think it should be before the transmute, not after.</p>



<a name="174664491"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/transmute%20vs%20from_raw_parts/near/174664491" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Hanna Kruppe <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/transmute.20vs.20from_raw_parts.html#174664491">(Sep 01 2019 at 14:51)</a>:</h4>
<p>That code block contains three completely separate ways to perform the type punning. The mem::forget at the end goes with the <code>from_raw_parts</code> only. But it's true that the first (transmute) example shouldn't clone. (But either way it wouldn't need a <code>mem::forget</code>, since transmute is by-value.)</p>



<a name="174664545"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/transmute%20vs%20from_raw_parts/near/174664545" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Hanna Kruppe <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/transmute.20vs.20from_raw_parts.html#174664545">(Sep 01 2019 at 14:52)</a>:</h4>
<p>As for why the transmute is UB, the simple reason is that <code>Vec</code> doesn't guarantee that such type punning is OK. There's also the fact that we don't guarantee any kind of determinism for layout of repr(Rust) structs (of which <code>Vec</code> is one), but for a stdlib type that's a technicality we could side-step if we wanted to.</p>



<a name="174665917"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/transmute%20vs%20from_raw_parts/near/174665917" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Shnatsel <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/transmute.20vs.20from_raw_parts.html#174665917">(Sep 01 2019 at 15:32)</a>:</h4>
<p><code>.as_mut_ptr()</code> does not consume the vector, merely returns a pointer, so when we call <code>.from_raw_parts()</code> in that example we actually have mutable aliasing, which I believe is UB</p>



<a name="174666945"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/transmute%20vs%20from_raw_parts/near/174666945" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Shnatsel <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/transmute.20vs.20from_raw_parts.html#174666945">(Sep 01 2019 at 16:00)</a>:</h4>
<p>I've run the first pattern through Clippy and it didn't complain, so I'll open an issue for Clippy as soon as this is clarified</p>



<a name="174667698"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/transmute%20vs%20from_raw_parts/near/174667698" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Hanna Kruppe <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/transmute.20vs.20from_raw_parts.html#174667698">(Sep 01 2019 at 16:21)</a>:</h4>
<p>Hmm, I would have said it's fine since the original Vec isn't used afterwards but it actually is, since it's passed to <code>mem::forget</code>.</p>



<a name="174668299"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/transmute%20vs%20from_raw_parts/near/174668299" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Shnatsel <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/transmute.20vs.20from_raw_parts.html#174668299">(Sep 01 2019 at 16:41)</a>:</h4>
<p>For some reason miri doesn't complain about this: <a href="https://play.rust-lang.org/?version=stable&amp;mode=debug&amp;edition=2018&amp;gist=87a6f1f8bc386f3c80b5f6157da1b161" target="_blank" title="https://play.rust-lang.org/?version=stable&amp;mode=debug&amp;edition=2018&amp;gist=87a6f1f8bc386f3c80b5f6157da1b161">https://play.rust-lang.org/?version=stable&amp;mode=debug&amp;edition=2018&amp;gist=87a6f1f8bc386f3c80b5f6157da1b161</a></p>



<a name="174668353"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/transmute%20vs%20from_raw_parts/near/174668353" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Hanna Kruppe <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/transmute.20vs.20from_raw_parts.html#174668353">(Sep 01 2019 at 16:43)</a>:</h4>
<p>presumably because Vec just (transitively) contains a <code>Unique</code> which is currently implemented as just a plain old raw pointer for aliasing questions</p>



<a name="174668412"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/transmute%20vs%20from_raw_parts/near/174668412" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Shnatsel <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/transmute.20vs.20from_raw_parts.html#174668412">(Sep 01 2019 at 16:45)</a>:</h4>
<p>So that's a bug in miri? Or rather, a limitation?</p>



<a name="174668617"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/transmute%20vs%20from_raw_parts/near/174668617" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Hanna Kruppe <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/transmute.20vs.20from_raw_parts.html#174668617">(Sep 01 2019 at 16:52)</a>:</h4>
<p>I wouldn't say that, miri is making the most of what's in the liballoc source code. It's kind of a liballoc/rustc issue in that <code>Vec</code>'s data pointer doesn't get any noalias superpowers in LLVM IR generation either, but some other factors block that too, so I guess miri is the main way that this can be observed today</p>



<a name="174669932"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/transmute%20vs%20from_raw_parts/near/174669932" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Shnatsel <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/transmute.20vs.20from_raw_parts.html#174669932">(Sep 01 2019 at 17:34)</a>:</h4>
<p>OK, I've opened an issue about the confusing docs: <a href="https://github.com/rust-lang/rust/issues/64073" target="_blank" title="https://github.com/rust-lang/rust/issues/64073">https://github.com/rust-lang/rust/issues/64073</a></p>



<a name="175748071"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/transmute%20vs%20from_raw_parts/near/175748071" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/transmute.20vs.20from_raw_parts.html#175748071">(Sep 15 2019 at 13:03)</a>:</h4>
<p>I'd say it is a Miri limitation, I'd like to make <code>Unique</code> meaningful eventually</p>



<a name="175748110"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/transmute%20vs%20from_raw_parts/near/175748110" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/transmute.20vs.20from_raw_parts.html#175748110">(Sep 15 2019 at 13:04)</a>:</h4>
<p>but so far I did not have a good idea for how to do that</p>



<a name="175748115"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/transmute%20vs%20from_raw_parts/near/175748115" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/transmute.20vs.20from_raw_parts.html#175748115">(Sep 15 2019 at 13:04)</a>:</h4>
<p><span class="user-mention" data-user-id="127617">@Shnatsel</span> ^</p>



<hr><p>Last updated: Aug 07 2021 at 22:04 UTC</p>
</html>